package com.daxt.common.config;

import com.daxt.base.common.util.RedisUtil;
import com.daxt.common.util.DateUtil;
import com.daxt.mapper.accept.ArchivesAcceptLogMapper;
import com.daxt.mapper.accept.CronMapper;
import com.daxt.model.dic.SystemConfigType;
import com.daxt.model.dic.VersionsMarkType;
import com.daxt.model.service.accept.vo.ArchivesAcceptLog;
import com.daxt.service.ArchivesAcceptService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;


@Configuration
@EnableScheduling
public class DynamicScheduleTaskConfig implements SchedulingConfigurer {

   @Autowired
   private  CronMapper cronMapper;
   @Autowired
   private ArchivesAcceptService acceptService;
   @Autowired
   private ArchivesAcceptLogMapper logMapper;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        if (RedisUtil.isAppoint(VersionsMarkType.VERSIONS_4)) {
            taskRegistrar.addTriggerTask(
                    //1.添加任务内容(Runnable)
                    () ->{
                        ArchivesAcceptLog acceptLog=new ArchivesAcceptLog();
                        acceptLog.setType("ws");
                        acceptLog.setCreateTime(DateUtil.getCurrentTime(DateUtil.DateFormat.YYYY_MM_DD_HH_mm_ss));
                        String startTime=DateUtil.getCurrentTime(DateUtil.DateFormat.YYYY_MM_DD);
                        String endTime=DateUtil.addDayOfMonth(startTime, DateUtil.DateFormat.YYYY_MM_DD,1);
                        try{
                            String code = acceptService.getWsAccept(null,null) ;
                            if("200".equals(code)){
                                acceptLog.setState("1");
                                acceptLog.setDescribes(startTime+"至"+endTime+"，的数据获取成功");
                            }else if ("201".equals(code)){
                                acceptLog.setDescribes(startTime+"至"+endTime+"，的数据获取失败，请添加对应分类");
                                acceptLog.setState("0");
                                System.out.println("接口调用失败，请请添加对应分类！！！！！");
                            }else{
                                acceptLog.setDescribes(startTime+"至"+endTime+"，的数据获取失败");
                                acceptLog.setState("0");
                                System.out.println("接口调用失败！！！！！");
                            }
                            logMapper.insert(acceptLog);

                            //删除临时文件，防止系统内存不够用
                            String ossDir = RedisUtil.getSysConfig(SystemConfigType.OSS_DIR);
                            String deletePath=ossDir+"/ws/";
                            File wsFile=new File(deletePath);
                            if (wsFile.exists()){
                                FileUtils.deleteDirectory(wsFile);
                            }
                            String wsDownloadPath=ossDir+"/wsDownload/";
                            File wsDownloaFile=new File(wsDownloadPath);
                            if (wsDownloaFile.exists()){
                                try {
                                    FileUtils.deleteDirectory(wsDownloaFile);
                                }catch (IOException io){
                                    io.printStackTrace();
                                }
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                            acceptLog.setDescribes(startTime+"至"+endTime+"，的数据获取失败");
                            acceptLog.setState("0");
                            logMapper.insert(acceptLog);
                            System.out.println("接口调用异常！！！！！");
                        }
                    },
                    //2.设置执行周期(Trigger)
                    triggerContext -> {
                        //2.1 从数据库获取执行周期
                        String cron = cronMapper.selectById(1).getCron();
                        //2.2 合法性校验.
                        if (StringUtils.isEmpty(cron)) {
                            // Omitted Code ..
                        }
                        //2.3 返回执行周期(Date)
                        return new CronTrigger(cron).nextExecutionTime(triggerContext);
                    }
            );
        }

    }

}
